package com.tony.server.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tony.easymvc.core.FileUtils;
import com.tony.easymvc.core.bean.ProjectModel;
import com.tony.easymvc.mapper.ProjectInfoMapper;
import com.tony.easymvc.po.ProjectInfo;
import com.tony.easymvc.po.ProjectInfoExample;
import com.tony.server.service.ProjectService;
import com.tony.vo.Result;
import com.tony.zip.ZipUtils;

@Controller
@RequestMapping("project")
public class ProjectInfoController {

    @Resource
    ProjectInfoMapper projectInfoMapper;
    @Resource
    ProjectService    projectService;

    @RequestMapping("select")
    @ResponseBody
    public ProjectInfo select(Integer id) {
        ProjectInfo pinfo = projectInfoMapper.selectByPrimaryKey(id);
        return pinfo;
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(ProjectInfo project) {
        try {
            project.setId(null);
            projectInfoMapper.insertSelective(project);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "保存失败 : " + e.getMessage(), null);

        }
        return new Result(true, "保存成功", project.getId());
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(ProjectInfo project) {
        projectInfoMapper.updateByPrimaryKeySelective(project);
        return new Result(true, "保存成功 ", project.getId());
    }

    @RequestMapping("list")
    @ResponseBody
    public List<ProjectInfo> list() {
        return projectInfoMapper.selectByExample(new ProjectInfoExample());
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id) {
        projectInfoMapper.deleteByPrimaryKey(id);
        return "删除成功";
    }

    @RequestMapping("create")
    @ResponseBody
    public String create(Integer id, HttpServletResponse response) {
        String sourceFileName = null;
        try {
            ProjectModel projectModel = projectService.startProject(id);
            sourceFileName = projectModel.getBasePath() + "/" + projectModel.getArtifactId();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment; filename=" + projectModel.getArtifactId() + ".zip");
            ZipUtils.zip(response.getOutputStream(), sourceFileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            FileUtils.delete(new File(sourceFileName));
        }
        return "生成结束";
    }

    public static void main(String[] args) {
        String s = "jdbc:mysql://192.168.31.106:3306/easymvc?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true".replaceAll("&",
                "&amp;");
        System.out.println(s);
    }

}
